package com.zigong.app.ui.utils;

import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.ruihang.generalibrary.ui.barrage.BarrageView;
import com.ruihang.ijkplaylib.widget.GPlayerScreenControllImp;
import com.ruihang.ijkplaylib.widget.GPlayerView;

import java.lang.ref.WeakReference;

/**
 * Created by liuyu on 2017/8/7.
 */

public class BarrageScreenController extends GSFPlayerScreenControllImp {

    private BarrageView barrageView;

    private boolean hasBarrage = true;
    private boolean isBarrageOpen;

    public BarrageScreenController(AppCompatActivity activity) {
        super(activity);
    }

    @Override
    public void onAddingView(GPlayerView playerView, LayoutInflater inflater, ViewGroup group) {
        AppCompatActivity ac = getActivityTemp();
        if (ac != null) {
            barrageView = new BarrageView(ac);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            group.addView(barrageView, lp);
        }
        isBarrageOpen = barrageView != null && barrageView.getVisibility() == View.VISIBLE;
        super.onAddingView(playerView, inflater, group);
    }

    public void addMsg(String msg) {
        if (barrageView != null) {
            barrageView.addMessage(msg);
        }
    }

    public void setBarrageState(boolean isOpen) {
        if (barrageView != null) {
            barrageView.setVisibility(isOpen ? View.VISIBLE : View.GONE);
            isBarrageOpen = isOpen;
        }
    }

    public boolean canBarrage() {
        return hasBarrage && isBarrageOpen;
    }

    public void setHasBarrage(boolean hasBarrage) {
        this.hasBarrage = hasBarrage;
    }

    public void setBarrageOpen(boolean barrageOpen) {
        isBarrageOpen = barrageOpen;
    }

    @Override
    public boolean hasBarrage() {
        return hasBarrage;
    }
}
